package cate.game.res;

import cate.common.util.GameResult;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@NoteClass("消耗选择")
public class CostSelect {

    @NoteField("选项列表")
    public List<Option> options;


    public void combineOptions() {
        List<Option> newOptions = new ArrayList<>();
        for (Option option : options) {
            if (newOptions.isEmpty()) {
                newOptions.add(option);
            } else {
                boolean exist = false;
                for (Option newOption : newOptions) {
                    if (option.tid == newOption.tid && option.type == newOption.type) {
                        if (newOption.uidList == null) {
                            newOption.uidList = new ArrayList<>();
                        }
                        if (option.uidList != null) {
                            newOption.uidList.addAll(option.uidList);
                        }
                        exist = true;
                        break;
                    }
                }
                if (!exist) {
                    newOptions.add(option);
                }
            }
        }
        options = newOptions;
    }

    /** 增加选项 */
    public void addOption(byte type, int tid, List<String> uidList) {
        if (options == null) {
            options = new ArrayList<>();
        }
        Option opt = new Option();
        opt.type = type;
        opt.tid = tid;
        opt.uidList = uidList;
        options.add(opt);
    }

    /** 根据对象大类型与限定条件获取指定的唯一ID列表 */
    public List<String> getUidList(byte type, int tid) {
        if (options == null){
            return null;
        }
        for (Option opt : options) {
            if (opt.type == type && opt.tid == tid) {
                return opt.uidList;
            }
        }
        return null;
    }

    /** 根据对象大类型获取指定的唯一ID列表 */
    public List<String> getUidLists(byte type) {
        if (options == null){
            return null;
        }
        combineOptions();
        for (Option opt : options) {
            if (opt.type == type) {
                return opt.uidList;
            }
        }
        return null;
    }

    public GameResult<Void> check() {
        GameResult<Void> r = new GameResult<>();
        Set<String> uidSet = new HashSet<>();
        if (options != null) {
            for (Option opt : options) {
                if (opt.uidList != null) {
                    for (String h : opt.uidList) {
                        if (uidSet.contains(h)) {
                            return r.fail("有重复选项！请勿使用外挂，已对该账号进行警示关注，可能会被封号！");
                        }
                        uidSet.add(h);
                    }
                }
            }
        }
        return r;
    }
}
